package com.hhit.bigscreenbackmanage.controller.tgygl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.common.Response;
import com.hhit.bigscreenbackmanage.entity.tgygl.*;
import com.hhit.bigscreenbackmanage.service.impl.tgygl.TgyglServiceImpl;
import com.hhit.bigscreenbackmanage.service.tgygl.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zhl
 * @Date 2021/7/22
 */
@RestController
@RequestMapping("/tgyglFlie")
public class TgyglFilecontroller {

    private static Logger log = LoggerFactory.getLogger(TgyglFilecontroller.class);

    @Autowired
    private TgyglServiceImpl tgyglService;

    @Autowired
    private TTgyglGcjzService tTgyglGcjzService; // 工程进展

    @Autowired
    private TTgyglGlgzService tTgyglGlgzService; // 管理工作

    @Autowired
    private TTgyglGzdpService tTgyglGzdpService; // 工作点评

    @Autowired
    private TTgyglZdgzService tTgyglZdgzService; // 重点工作

    @Autowired
    private TTgyglKeyWorkService tTgyglKeyWorkService; // 重点工作

    @Autowired
    private TTgyglConverterStationService tTgyglConverterStationService; //t_tgygl_converter_station表

    /**
     * 查询文件列表
     * @return
     */
    @GetMapping("/queryTgyglFile")
    public Response<Map<String, Object>> queryTgyglFile(@RequestParam(value = "nf",required = false) String nf,
                                                        @RequestParam(value = "yf",required = false) String yf,
                                                        @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                                        @RequestParam(value = "pageNo",required = false) Integer pageNo){
        try {
            JSONObject jsonObject = new JSONObject();
            if (ObjectUtil.isNotEmpty(nf)){
                jsonObject.put("nf",nf);
            }
            if (ObjectUtil.isNotEmpty(yf)){
                jsonObject.put("yf",yf);
            }
            jsonObject.put("pageSize",pageSize);
            jsonObject.put("pageNo",pageNo);
            Map<String, Object> stringObjectMap = tgyglService.queryAllByLimit(jsonObject);
            return Response.createResponseBySuccess("查询成功",stringObjectMap);
        }catch (Exception e){
            return Response.createResponseByError("查询失败:"+e.getMessage());
        }
    }

    /**
     * 删除文件
     * @return
     */
    @GetMapping("/deleteTgyglFile")
    public Response<Map<String, Object>> deleteTgyglFile(@RequestParam(value = "id",required = false) Integer id){
        Map<String, Object> stringObjectMap = new HashMap<String, Object>();
        try {
            if (id!=null) {
                int code = tgyglService.deleteByPrimaryKey(id);
                stringObjectMap.put("flag", true);
            }
            return Response.createResponseBySuccess("删除成功",stringObjectMap);
        }catch (Exception e){
            stringObjectMap.put("flag", false);
            return Response.createResponseByError("删除失败:"+e.getMessage());
        }
    }

    /**
     * 查询文件中的信息
     * @return
     */
    @GetMapping("/queryTgyglInfos")
    public Response<Map<String, Object>> queryTgyglInfos(@RequestParam(value = "fileId",required = false) Integer fileId){
        try {
            JSONObject jsonObject = new JSONObject();
//            if (ObjectUtil.isNotEmpty(nf)){
//                jsonObject.put("nf",nf);
//            }
//            if (ObjectUtil.isNotEmpty(yf)){
//                jsonObject.put("yf",yf);
//            }
//            jsonObject.put("pageSize",pageSize);
//            jsonObject.put("pageNo",pageNo);
            if (fileId==null) {
                return Response.createResponseByError("查询失败: 请选择文件!");
            }
            //查询文件
            TgyglFileEntity fileEntity = tgyglService.selectByPrimaryKey(fileId);
            //查询工程进展信息
            List<TTgyglGcjz> gcjzInfos =  tTgyglGcjzService.selectListByFileId(fileId);
            // 查询管理工作信息
            List<TTgyglGlgz> glgzInfos = tTgyglGlgzService.selectListByFileId(fileId);
            // 查询工作点评信息
            List<TTgyglGzdp> gzdpInfos = tTgyglGzdpService.selectListByFileId(fileId);
            // 查询重点工作信息
            List<TTgyglZdgz> zdgzInfos = tTgyglZdgzService.selectListByFileId(fileId);

            Map<String, Object> stringObjectMap = new HashMap<String, Object>();
            stringObjectMap.put("gcjzTitle", fileEntity.getGcjzTitle());
            stringObjectMap.put("gcjzData", gcjzInfos);

            stringObjectMap.put("glgzTitle", fileEntity.getGlgzTitle());
            stringObjectMap.put("glgzData", glgzInfos);

            stringObjectMap.put("gzdpTitle", fileEntity.getGzdpTitle());
            stringObjectMap.put("gzdpData", gzdpInfos);

            stringObjectMap.put("zdgzTitle", fileEntity.getZdgzTitle());
            stringObjectMap.put("zdgzData", zdgzInfos);

            return Response.createResponseBySuccess("查询成功",stringObjectMap);

        }catch (Exception e){
            return Response.createResponseByError("查询失败:"+e.getMessage());
        }
    }

    /**
     * 查询文件中的附件表格
     * @return
     */
    @GetMapping("/queryTgyglFjTables")
    public Response<Map<String, Object>> queryTgyglFjTables(@RequestParam(value = "fileId",required = false) Integer fileId){
        try {
            if (fileId==null) {
                return Response.createResponseByError("查询失败: 请选择文件!");
            }
            //查询文件
            TgyglFileEntity fileEntity = tgyglService.selectByPrimaryKey(fileId);
            //查询t_tgygl_converter_station数据
            List<TTgyglConverterStation> tTgyglConverterStations =
                    tTgyglConverterStationService.selectListByFileId(fileId);

//            //查询工程进展信息
//            List<TTgyglGcjz> gcjzInfos =  tTgyglGcjzService.selectListByFileId(fileId);
//            // 查询管理工作信息
//            List<TTgyglGlgz> glgzInfos = tTgyglGlgzService.selectListByFileId(fileId);
//            // 查询工作点评信息
//            List<TTgyglGzdp> gzdpInfos = tTgyglGzdpService.selectListByFileId(fileId);
//            // 查询重点工作信息
//            List<TTgyglZdgz> zdgzInfos = tTgyglZdgzService.selectListByFileId(fileId);

            Map<String, Object> stringObjectMap = new HashMap<String, Object>();
//            stringObjectMap.put("gcjzTitle", fileEntity.getGcjzTitle());
//            stringObjectMap.put("gcjzData", gcjzInfos);
//
//            stringObjectMap.put("glgzTitle", fileEntity.getGlgzTitle());
//            stringObjectMap.put("glgzData", glgzInfos);
//
//            stringObjectMap.put("gzdpTitle", fileEntity.getGzdpTitle());
//            stringObjectMap.put("gzdpData", gzdpInfos);
//
//            stringObjectMap.put("zdgzTitle", fileEntity.getZdgzTitle());
//            stringObjectMap.put("zdgzData", zdgzInfos);
            stringObjectMap.put("tableTitles", fileEntity.getFjTableTitles().split(","));
            stringObjectMap.put("converterStation", tTgyglConverterStations);

            return Response.createResponseBySuccess("查询成功",stringObjectMap);

        }catch (Exception e){
            return Response.createResponseByError("查询失败:"+e.getMessage());
        }
    }

    /**
     * 获取下月重点工作内容
     * @param JSONObject
     * @return
     */
    @RequestMapping("/getTTgyglKeyWork")
    public Response getTTgyglKeyWork(@RequestBody JSONObject JSONObject) {
        try {
            return Response.createResponseBySuccess("查询成功",tTgyglKeyWorkService.getTTgyglKeyWork(JSONObject));
        }catch (Exception e){
            return Response.createResponseByError("查询失败:"+e.getMessage());
        }
    }

    /**
     * 工作点评统计
     * @param json
     * @return
     */
    @RequestMapping("/getGZDPTj")
    public Response getGZDPTj(@RequestBody JSONObject json) {
        try {
            return Response.createResponseBySuccess("查询成功",tTgyglKeyWorkService.getGZDPTj(json));
        }catch (Exception e){
            return Response.createResponseByError("查询失败:"+e.getMessage());
        }
    }

    /**
     * 新增下月重点工作内容
     * @param json
     * @return
     */
    @RequestMapping("/insertTTgyglKeyWork")
    public Response insertTTgyglKeyWork(@RequestBody JSONObject json) {
        try {
            int res = tTgyglKeyWorkService.insertTTgyglKeyWork(json);
            if (res>0){
                return Response.createResponseBySuccess("新增成功",res);
            }else {
                return Response.createResponseBySuccess("新增失败",res);
            }
        }catch (Exception e){
            return Response.createResponseByError("新增失败:"+e.getMessage());
        }
    }

}
